class Solution(object):
    def maxDistance(self, colors):
        n = len(colors)
        if colors[0] != colors[n - 1]:
            return n - 1
        else:
            for i in range(1, n - 1):
                if colors[i] != colors[0]:
                    return n - 1 - i
                if colors[n - 1 - i] != colors[0]:
                    return n - 1 - i


